// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   Music.java

package com.kaylyn.util;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.Random;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

public class Music {

	public Music() {
	}

	public static void start() {
		r = new Runnable() {

			public void run() {
				Music.random = new Random(System.currentTimeMillis());
				do
					try {
						if (!Music.isClosed
								&& Music.getState().equals("run")
								&& (Music.current == null || Music.current
										.available() == 0))
							Music.next();
						Thread.sleep(2000L);
					} catch (InterruptedException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
				while (true);
			}

		};
		(new Thread(r)).start();
	}

	public static void pause() {
		AudioPlayer.player.stop(current);
		setState("pause");
	}

	public static void run() {
		AudioPlayer.player.start(current);
		setState("run");
	}

	public static void next() {
		int i;

		i = -1;
		if (file.length > 0)
			i = random.nextInt(file.length);

		next(i);
	}

	public static void next(int i) {
		if (i < 0 || i > file.length)
			return;
		if (i == currentFile)
			return;
		try {
			pause();
			if (current != null) {
				current.close();
				current = null;
				isClosed = true;
			}
			current = getAudioStream(i);
			isClosed = false;
			run();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String getState() {
		return state;
	}

	private static void setState(String s) {
		state = s;
	}

	private static AudioStream getAudioStream(int index) throws IOException {
		java.io.InputStream inputStream = Music.class
				.getResourceAsStream("music/" + file[index] + ".au");
		currentFile = index;
		return new AudioStream(new BufferedInputStream(inputStream));
	}

	private static AudioStream current = null;
	private static int currentFile = -1;
	private static String state = "run";
	private static String file[] = new String[] {};
	private static Runnable r;
	private static boolean isClosed;
	private static Random random;

}
